package movenment2copy;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

/**
 * This class implements KeyListener and is basically used to filter keystrokes
 * that aren't used by the game.
 * The class-variable lastKeyUsed contains the last key used and is called for
 * by the game each time the worm has to move forward.
 * @author Jonatan
 */
public class CustomListener implements KeyListener {

	  private int lastKeyUsed = 39; // Initially 39 which is the right arrow-key.
	
	  public void keyPressed(KeyEvent e) {
	    	dumpInfo("Pressed", e);
	  }
	
	  @Override
	  public void keyReleased(KeyEvent e) {
	  }
	
	  public void keyTyped(KeyEvent e) {
	    dumpInfo("Typed", e);
	  }
	
	  /**
	   * Utilized by the keyEvent-methods to dump information.
	   * @param s String that represents what type of event occured. Not used by the game.
	   * @param e The KeyEvent that occured. 
	   */
	  private void dumpInfo(String s, KeyEvent e) {
	    if(e.getKeyCode() == KeyEvent.VK_LEFT){
	    	lastKeyUsed = 37;
	    }else if(e.getKeyCode() == KeyEvent.VK_RIGHT){
	    	lastKeyUsed = 39;
	    }else if(e.getKeyCode() == KeyEvent.VK_UP){
	    	lastKeyUsed = 38;
	    }else if(e.getKeyCode() == KeyEvent.VK_DOWN){
	    	lastKeyUsed = 40;
	    }
	  }

	  /**
	   * Returns the last key used to the game-class.
	   * @return lastKeyUsed.
	   */
	  public int getLastKeyUsed(){
		  return this.lastKeyUsed;
	  }
}
